package com.sysu.simulation.fragments.sim;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sysu.simulation.BaseFragment;
import com.sysu.simulation.R;
import com.sysu.simulation.SimFragment;
import com.sysu.simulation.bean.Environment;
import com.sysu.simulation.bean.SimParams;
import com.sysu.simulation.bean.Stage;
import com.sysu.simulation.interfaces.SeriesFragment;
import com.sysu.simulation.widget.SimStageCard;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;


public class SimStageFragment extends BaseFragment implements SeriesFragment, SimStageCard.OnCloseCardListener,
        SimFragment.SimParamsSetter{
    private List<SimStageCard> cards;
    private List<Stage> stages;
    private Environment env;

    @BindView(R.id.sim_temp)
    EditText mTemp;
    @BindView(R.id.sim_humidity)
    EditText mHumidity;
    @BindView(R.id.sim_stage_container)
    LinearLayout stageContainer;
    @BindView(R.id.sim_stage_add)
    ImageView addStage;

    public SimStageFragment() {

    }

    public static SimStageFragment newInstance() {
        return new SimStageFragment();
    }

    @Override
    protected void initWidget(View root) {
        cards = new ArrayList<>();
        addCard();

        addStage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addCard();
            }
        });
    }

    private void addCard() {
        SimStageCard card = new SimStageCard(mContext);
        card.setOnCloseCardListener(this);
        cards.add(card);
        stageContainer.addView(card, cards.size()-1);
        card.setNumber(cards.size());
    }

    @Override
    protected void initData() {
        stages = new ArrayList<>();
        env = new Environment();
    }

    @Override
    public String getPreviousPage() {
        return SimClothesFragment.class.getName();
    }

    @Override
    public String getNextPage() {
        return SimSubmitFragment.class.getName();
    }

    @Override
    public String getNavTitle() {
        return "运动状态设置";
    }

    @Override
    public Boolean canMoveForward() {
        boolean cancel = false;
        View focusView = null;

        mTemp.setError(null);
        mHumidity.setError(null);
        String simTemp = mTemp.getText().toString().trim();
        String simHumidity = mHumidity.getText().toString().trim();

        if (TextUtils.isEmpty(simHumidity)) {
            mHumidity.setError(getString(R.string.error_field_required));
            focusView = mHumidity;
            cancel = true;
        }

        if (TextUtils.isEmpty(simTemp)) {
            mTemp.setError(getString(R.string.error_field_required));
            focusView = mTemp;
            cancel = true;
        }

        if(cancel) {
            focusView.requestFocus();
            return false;
        }

        env.setTemperature(Integer.parseInt(simTemp));
        env.setHumidity(Integer.parseInt(simHumidity));

        stages.clear();
        for(SimStageCard card : cards) {
            String sp = card.getSpeed();
            String dur = card.getDuration();

            if (TextUtils.isEmpty(dur)) {
                card.getEditDuration().setError(getString(R.string.error_field_required));
                focusView = card.getEditDuration();
                cancel = true;
            }

            if (TextUtils.isEmpty(sp)) {
                card.getEditSpeed().setError(getString(R.string.error_field_required));
                focusView = card.getEditSpeed();
                cancel = true;
            }

            if(cancel) {
                focusView.requestFocus();
                return false;
            }

            Stage st = new Stage();
            st.setSpeed(Float.parseFloat(sp));
            st.setDuration(Integer.parseInt(dur));
            stages.add(st);
        }

        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sim_stage;
    }

    @Override
    public void onCloseCard(int number) {
        if(cards.size()>1) {
            stageContainer.removeViewAt(number - 1);
            cards.remove(number - 1);
            for (int i = number - 1; i < cards.size(); i++) {
                cards.get(i).setNumber(i + 1);
            }
        }
    }

    @Override
    public void setParams(SimParams params) {
        params.setEnv(env);
        params.setStages(stages);
    }
}
